package com.bdqn.t320.service;

import com.bdqn.t320.dao.AttachMapper;
import com.bdqn.t320.dao.SysUserMapper;
import com.bdqn.t320.pojo.Attach;
import com.bdqn.t320.pojo.Page;
import com.bdqn.t320.pojo.SysUser;
import com.bdqn.t320.util.MyLog;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.naming.Name;
import java.util.List;

@Service
public class SysUserServiceImp implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private AttachMapper attachMapper;

    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @MyLog(value = "aaaaaaa")
    public List<SysUser> getUserListBiz(Page page, SysUser sysUser) {

        return sysUserMapper.getUserList(sysUser, (page.getCurrentPage() - 1) * page.getPageSize(), page.getPageSize());
    }

    @Override
    @MyLog
    public Integer getCountBiz(SysUser sysUser) {
        return sysUserMapper.getCount(sysUser);
    }


    @Override
    public int add(SysUser sysUser) {
        return sysUserMapper.add(sysUser);
    }

    @Override
    public boolean adds(List<SysUser> list) {
        boolean result = true;
        try {
            for (SysUser sysUser : list) {
                if (sysUserMapper.add(sysUser) < 1) {
                    result = false;
                }
                //System.out.println(1 / 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
            throw e;
        }
        return result;
    }

    @Override
    public SysUser loginBiz(String userCode) {
        return sysUserMapper.login(userCode);
    }

    @Override
    public SysUser viewBiz(Integer id) {
        return sysUserMapper.view(id);
    }

    @Override
    public int userCodeExistBiz(String userCode) {
        return sysUserMapper.userCodeExist(userCode);
    }

    @Override
    public int deleteByIdBiz(Integer id) {
        return sysUserMapper.deleteById(id);
    }

    @Transactional
    @Override
    public boolean addUserAttach(SysUser sysUser, Attach attach) {
        int attachCount = 0;
        boolean bool = false;
        try {
            int userCount = sysUserMapper.add(sysUser);
            if (attach != null && userCount > 0) {
                attachCount = attachMapper.add(attach);
            }
            bool = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bool;
    }

    @Override
    public int updateBiz(SysUser user) {
        return sysUserMapper.update(user);
    }
}

